package ynu.edu.model.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import ynu.edu.common.domain.PageDTO;
import ynu.edu.model.entity.Text2Img;
import ynu.edu.model.mapper.Text2ImgMapper;
import ynu.edu.model.service.Text2ImgService;



import java.time.LocalDateTime;
import java.util.*;

/**
 * Stable Diffusion服务实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class Text2ImgServiceImpl implements Text2ImgService {


    private final Text2ImgMapper text2ImgMapper;


    @Override
    public PageDTO<Text2Img> getPageRecords(Integer page, Integer size, String status, String modelName) {
        Page<Text2Img> pageParam = new Page<>(page, size);
        
        QueryWrapper<Text2Img> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        
        if (status != null && !status.trim().isEmpty()) {
            queryWrapper.eq("status", status);
        }
        
        if (modelName != null && !modelName.trim().isEmpty()) {
            queryWrapper.eq("model_name", modelName);
        }
        
        Page<Text2Img> result = text2ImgMapper.selectPage(pageParam, queryWrapper);
        return PageDTO.of(result, Text2Img.class);
    }
    
    @Override
    public Text2Img getByTaskId(String taskId) {
        QueryWrapper<Text2Img> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("task_id", taskId);
        return text2ImgMapper.selectOne(queryWrapper);
    }
    
    @Override
    public Map<String, Object> getStatistics() {
        try {
            Map<String, Object> stats = new HashMap<>();
            stats.put("totalRecords", text2ImgMapper.selectCount(null));
            
            // 成功记录数
            QueryWrapper<Text2Img> successWrapper = new QueryWrapper<>();
            successWrapper.eq("status", "success");
            Integer successCount = text2ImgMapper.selectCount(successWrapper);
            stats.put("successRecords", successCount);
            
            // 失败记录数
            QueryWrapper<Text2Img> failedWrapper = new QueryWrapper<>();
            failedWrapper.eq("status", "failed");
            Integer failedCount = text2ImgMapper.selectCount(failedWrapper);
            stats.put("failedRecords", failedCount);
            
            // 计算成功率
            int total = text2ImgMapper.selectCount(null);
            if (total > 0) {
                double successRate = (double) successCount / total * 100;
                stats.put("successRate", String.format("%.2f%%", successRate));
            } else {
                stats.put("successRate", "0.00%");
            }
            
            return stats;
        } catch (Exception e) {
            log.error("获取统计信息失败", e);
            throw new RuntimeException("获取统计信息失败: " + e.getMessage(), e);
        }
    }
    
    @Override
    public boolean deleteByTaskId(String taskId) {
        QueryWrapper<Text2Img> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("task_id", taskId);
        int result = text2ImgMapper.delete(queryWrapper);
        return result > 0;
    }
} 